package com.mingdao.lhy.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mingdao.lhy.mapper.wish.XueJiMapper;
import com.mingdao.lhy.pojo.XueJi;
import com.mingdao.lhy.util.ReadExcel;
import com.mingdao.lhy.util.Validate;

@Service
public class ImportServiceImpl implements ImportService {

	private static Logger log = LoggerFactory.getLogger(ImportServiceImpl.class);
	private static String mzstr = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57";

	@Autowired
	private XueJiMapper xueJiMapper;

	// @Autowired
	// private ReadExcel readExcel;

	/**
	 * 导入学籍数据
	 */
	@Override
	public List<String> importStatus(String fileName, int startRow, String byzxdm) {

		int result = -1;
		List<String> retList = new ArrayList<String>();
		List<XueJi> data = new ArrayList<XueJi>();

		// 创建处理EXCEL的类
		ReadExcel readExcel = new ReadExcel();
		// 解析excel，获取上传的事件单
		List<Map<String, Object>> xueJiList = readExcel.getExcelInfo(fileName);

		if (null == xueJiList) {
			log.debug("excel文件未能正常打开！");
			return null;
		}

		XueJi xueJi = null;
		int count = 0;
		boolean isOk = true;
		Map<String, String> sfzhMap = new CaseInsensitiveMap();
		/**
		 * 先将原来的数据全部删除
		 */
		xueJiMapper.deleteByByzxdm(byzxdm);

		// 至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
		for (Map<String, Object> obj : xueJiList) {

			count++;

			xueJi = new XueJi();
			String str0 = (((String) obj.get("0")) + "").trim();
			String str1 = (((String) obj.get("1")) + "").trim();
			String str2 = (((String) obj.get("2")) + "").trim();
			String str3 = (((String) obj.get("3")) + "").trim();
			String str4 = (((String) obj.get("4")) + "").trim();
			String str5 = (((String) obj.get("5")) + "").trim();
			String str6 = (((String) obj.get("6")) + "").trim();
			String str7 = (((String) obj.get("7")) + "").trim();

			// 学籍号
			if (null == str0 || "".equals(str0.trim())) {
				retList.add("第" + count + "条数据学籍号不能为空<br>");
				isOk = false;
				continue;

			}
			xueJi.setXjh(str0.trim());

			// 姓名
			if (null == str1 || "".equals(str1)) {
				retList.add("第" + count + "条数据学生姓名不能为空<br>");
				isOk = false;
				continue;

			}
			xueJi.setXm(str1);

			// 身份证件号
			if (null == str2 || "".equals(str2) || !Validate.isSfzhOk(str2)) {
				retList.add("第" + count + "条数据学生身份证号有误：" + str2 + "<br>");
				isOk = false;
				continue;

			}
			if (null != sfzhMap.get(str2)) {
				retList.add("第" + count + "条数据学生身份证号重复：" + str2 + "<br>");
				isOk = false;
				continue;

			}
			sfzhMap.put(str2, str2);
			xueJi.setSfzh(str2.toUpperCase());

			// 班级名称
			if (null == str3 || "".equals(str3)) {
				retList.add("第" + count + "条数据学生班级名称不能为空<br>");
				isOk = false;
				continue;

			}
			xueJi.setBjdm(str3);

			// 性别
			if (null == str4 || "".equals(str4)) {
				retList.add("第" + count + "条数据学生性别不能为空<br>");
				isOk = false;
				continue;

			}
			if (!("1".equals(str4) || "2".equals(str4))) {
				retList.add("第" + count + "条数据学生性别只能为1或2<br>");
				isOk = false;
				continue;

			}
			xueJi.setXb(str4);

			// 民族
			str5 = str5.replace("'", "");
			if (null == str5 || "".equals(str5)) {
				retList.add("第" + count + "条数据学生民族不能为空<br>");
				isOk = false;
				continue;

			}
			if (mzstr.indexOf(str5) < 0) {
				retList.add("第" + count + "条数据学生民族代码不正确<br>");
				isOk = false;
				continue;

			}
			xueJi.setMz(str5);

			// 出生年月
			if (null == str6 || "".equals(str6)) {
				retList.add("第" + count + "条数据学生出生年月不能为空<br>");
				isOk = false;
				continue;

			}
			// 根据身份证来生成出生日期
			xueJi.setCsrq(str2.substring(6, 14));

			// 学生状态
			if (null == str7 || "".equals(str7) || !"正常".equals(str7)) {
				retList.add("第" + count + "条数据学生状态只能为正常：" + str7 + "<br>");
				isOk = false;
				continue;

			}
			xueJi.setXszt(str7);

			// 问题类型
			xueJi.setWtlx("");

			// 错误类型
			xueJi.setCwlx("");

			// 毕业中学校代码
			xueJi.setByzxdm(byzxdm);

			// 密码默认为身份证号后6位
			xueJi.setPassword(xueJi.getSfzh().substring(10, 18));

			data.add(xueJi);

			log.debug("第" + count + "条数据：" + xueJi.toString());

		}

		// 如果所有数据都OK才进行导入操作
		if (isOk) {
			count = -1;
			for (XueJi obj : data) {
				count++;
				try {
					result = xueJiMapper.insert(obj);
					if (result != 1) {
						log.error("第" + count + "条学籍数据保存失败：" + obj.toString());

					}
				} catch (Exception e) {
					System.out.println(e.getMessage());
					retList.add("第" + count + "条数据学生不正常：" + obj.getSfzh() + "<br>");
				}
			}

			log.debug("一共导入 " + count + " 条记录。");
			return retList;
		} else {
			// 如果有任何数据不OK就将错误数据相关信息返回
			return retList;
		}

	}

}
